#include "stdafx.h"
#include "Obj.h"
#include "ObjContainer.h"
#include "player/Player.h"

Obj::Obj(const glm::vec3 & pos) : m_pos(pos), toBeDeleted(false)
{
	type = type_obj;
    static short idCounter = 1;
    m_pickColor = idCounter++;
	
	ObjContainer::getInstance()->addObj(this);
}

Obj::~Obj() {}

bool Obj::hasPickColor(unsigned short pickColor)
{
    return pickColor == m_pickColor;
}

glm::ivec2 Obj::getScreenPos(const glm::mat4& viewMatrix)
{
	glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0), m_pos); // create a world matrix 
	glm::mat4 projMatrix = Game::getPerspMatrix();

	glm::vec4 localPos(0.0f, 0.0f, 0.0f, 1.0);
	glm::mat4 mvpMatrix = projMatrix * viewMatrix * modelMatrix;
	glm::vec4 viewPos = mvpMatrix * localPos;
	
	glm::ivec2 screenPos(Game::windowDim.x*(0.5f + 0.5f*viewPos.x/viewPos.w), Game::windowDim.y*(0.5 + 0.5f*viewPos.y/viewPos.w)); // [-n,+n]->[0,1]
	return screenPos;
}
